<?php defined('IN_LK') or die('Access Denied');
/**
 * LK 参数处理类
 * @ category   LK
 * @ package  LK
 * @ subpackage  core
 * @ author <yangzhiguo0903@163.com>
 * @ version  $Id$
 */
class param
{
	/**
	 * 默认参数设置
	 */
	private $config = array();
	
	public function __construct()
	{
		if(!get_magic_quotes_gpc())
		{
			if(isset($_POST))
			{
				$_POST = new_addslashes($_POST);
			}
			if(isset($_GET))
			{
				$_GET = new_addslashes($_GET);
			}
			if(isset($_REQUEST))
			{
				$_REQUEST = new_addslashes($_REQUEST);
			}
			if(isset($_COOKIE))
			{
				$_COOKIE = new_addslashes($_COOKIE);
			}
		}
		$this->config = load_config('base');
		/**
		 * 额外参数处理
		 */
		if(isset($this->config['default']['data']) && !empty($this->config['default']['data']) 
		&& is_array($this->config['default']['data']))
		{
			$data = $this->config['default']['data'];
			foreach($data as $_key => $_value)
			{
				if(!isset($_GET[$_key]))
				{
					$_GET[$_key] = $_value;
				}
			}
		}
		unset($this->config['default']['data']);
	}
	
   /**
    * 根据URL参数获取模型,控制器,方法
	* @return array
    */
	public function get()
	{
		$param = $this->config['default'];
		switch($this->config['url_mode'])
		{
			case 1:  //普通url路由
				if(isset($_GET['m']) && !empty($_GET['m']))
				{
					$param['m'] = $_GET['m'];
				}
				if(isset($_GET['c']) && !empty($_GET['c']))
				{
					$param['c'] = $_GET['c'];
				}
				if(isset($_GET['a']) && !empty($_GET['a']))
				{
					$param['a'] = $_GET['a'];
				}
				break;
			case 2:  //path模式
				self::get_pathinfo();
				if(1)
				{
					$paths = explode('/', trim($_SERVER['PATH_INFO'],'/'));
					$paths_size = count($paths);
					$m = array_shift($paths);
					$param['m'] = $paths_size> 0 && $m ? $m : $param['m'];
					$param['c'] = $paths_size> 1 ? array_shift($paths) : $param['c'];
					$param['a'] = $paths_size> 2 ? array_shift($paths) : $param['a'];
					$param_size = count($paths);
					for($i = 0; $i <$param_size; $i ++)
					{
						if(isset($paths[1+$i]))
						{
							if(!isset($param[$paths[$i]]))
							{
								$param[$paths[$i]] = $paths[++$i];
							}
						}
					}
					unset($paths, $paths_size, $param_size);
				}
				else
				{
					$pathinfo = array();
					preg_replace('@(\w+)/([^,\/]+)@e', '$pathinfo[\'\\1\']="\\2";', $_SERVER['PATH_INFO']);
					if(!empty($pathinfo))
					{
						$param = array_merge($param, $pathinfo);
					}
				}
				$GET = $_GET;
				$_GET = array_merge($GET, new_rawurlencode($param));
				unset($GET);
				break;
		}
		return $param;
	}
	
   /**
    * 获取服务器的PATH_INFO信息 get_pathinfo
	* @return null
	*/
	public static function get_pathinfo()
	{
		$path = '';
		if(isset($_SERVER['PATH_INFO']) && !empty($_SERVER['PATH_INFO']))
		{
			if(0 === strpos($_SERVER['PATH_INFO'], $_SERVER['SCRIPT_NAME']))
			{
				$path = substr($_SERVER['PATH_INFO'], strlen($_SERVER['SCRIPT_NAME']));
			}
			else
			{
				$path = $_SERVER['PATH_INFO'];
			}
		}
		elseif(isset($_SERVER['ORIG_PATH_INFO']) && !empty($_SERVER['ORIG_PATH_INFO']))
		{
			if(0 === strpos($_SERVER['ORIG_PATH_INFO'], $_SERVER['SCRIPT_NAME']))
			{
				$path = substr($_SERVER['ORIG_PATH_INFO'], strlen($_SERVER['SCRIPT_NAME']));
			}
			else
			{
				$path = $_SERVER['ORIG_PATH_INFO'];
			}
		}
		elseif(isset($_SERVER['REDIRECT_PATH_INFO']) && !empty($_SERVER['REDIRECT_PATH_INFO']))
		{
			$path = $_SERVER['REDIRECT_PATH_INFO'];
		}
		$_SERVER['PATH_INFO'] = empty($path) ? '/' : $path;
	}
}